﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PessMvc.Helpers;
using DoddleReport;
using DoddleReport.Web;
using PessMvc.Models;

namespace PessMvc.Controllers
{
    [Authorize]
    [NotNeedToChangePassword]
    public class CommentController : Controller
    {
        //
        // GET: /Comment/
        PessModel.PESSEntities db;
        public CommentController()
        {
            db = new PessModel.PESSEntities();
        }
        [Authorize(Roles="Admin")]
        public ActionResult Export2Excel(int expertGroupID)
        {
            var expertGroup = db.ExpertGroups.Find(expertGroupID);
            var query = (from a in expertGroup.Archives
                    join c in db.ExpertComments on a.ID equals c.ArchiveID
                    select new { ArchiveSN = a.SerialNumber, AuthorName = a.TeacherName, c.Comment, ExpertJudge = c.ExpertJudge.RealName })
                    .AsEnumerable();

            //Activity activity = db.Activities.Find(activityID);
            //Subject subject = db.Subjects.Find(subjectID);
            string subTitle = string.Format("{0}-{1}-{2}", 
                expertGroup.Callsign,
                expertGroup.Subject.Name,
                expertGroup.Subject.Activity.Name
                );
            var report = Utility.CreateReport(query,null, "评语汇总", subTitle);
            IReportWriter writer = new DoddleReport.Writers.ExcelReportWriter();
            return new ReportResult(report, writer, "application/vnd.ms-excel") { FileName = HttpUtility.UrlPathEncode(subTitle+".xls") }; 
            //return View();
        }

    }
}
